package org.dzq.designmode.singleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @ClassName: HungrySingleton
 * @Package: org.dzq.designmode.singleton
 * @author: 越
 * @date: 2023/6/12 20:22
 * @Description: 预加载饿汉式单例模式
 */
public class HungrySingleton implements Serializable {

    private static final long serialVersionUID = -1L;

    private static HungrySingleton instance = new HungrySingleton();

    private HungrySingleton(){
        if(instance!=null){
            throw new RuntimeException("this is singleton");
        }
    }

    public static HungrySingleton getInstance() {
        return instance;
    }

    Object readResolve() throws ObjectStreamException {
        return getInstance();
    }
}
